package com.example.workflow.security;



import com.example.workflow.pojo.Result;
import com.example.workflow.util.WebUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
//        ObjectMapper objectMapper = new ObjectMapper();
//        Result<String> result = new Result<String>(HttpStatus.FORBIDDEN.value(), "权限不足", "权限不足");
//        String json = objectMapper.writeValueAsString(result);
//        WebUtils.renderString(response,json);
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("{\"code\":\"403\",\"message\":\"权限不足\",\"data\":\"权限不足\"}");
    }
}

